package com.sinoglobal.dumping.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.sinoglobal.dumping.R;
import com.sinoglobal.dumping.activity.Dumpling_CongratulationCardActivity;
import com.sinoglobal.dumping.activity.Dumpling_MainActivity;
import com.sinoglobal.dumping.activity.Dumpling_MakeDumplingActivity;
import com.sinoglobal.dumping.base.Dumpling_NetWorkUtil;
import com.sinoglobal.dumping.base.Dumpling_SinoBaseFragment;

/**
 * 包饺子页面
 * Created by Administrator on 2015/10/30.
 */
public class Dumpling_MakeDumplingFragment extends Dumpling_SinoBaseFragment{
    private View view, noNet, hasNet;                                         //无网络视图和有网络视图
    private ImageView dumpling_make_main_friend, dumpling_make_person, dumpling_make_main_money, dumpling_make_main_card;//朋友 有缘人 现金  贺卡
    private LinearLayout dumpling_make_main_moneyLayout, dumpling_make_main_cardLayout;//钱 贺卡
    private boolean friend_flag = false;
    private Animation animation;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.dumpling_make_dumpling_fragment, container, false);
        init();
        return view;
    }

    private void init(){
        dumpling_make_main_friend = (ImageView) view.findViewById(R.id.dumpling_make_main_friend);
        dumpling_make_person = (ImageView) view.findViewById(R.id.dumpling_make_person);
        dumpling_make_main_money = (ImageView) view.findViewById(R.id.dumpling_make_main_money);
        dumpling_make_main_card = (ImageView) view.findViewById(R.id.dumpling_make_main_card);
        dumpling_make_main_moneyLayout = (LinearLayout) view.findViewById(R.id.dumpling_make_main_moneyLayout);
        dumpling_make_main_cardLayout = (LinearLayout) view.findViewById(R.id.dumpling_make_main_cardLayout);
        dumpling_make_main_friend.setOnClickListener(this);
        dumpling_make_person.setOnClickListener(this);
        dumpling_make_main_money.setOnClickListener(this);
        dumpling_make_main_card.setOnClickListener(this);

        noNet = view.findViewById(R.id.dumpling_make_no_network);
        hasNet = view.findViewById(R.id.dumpling_make_have_network);
        checkNetwork();
        noNet.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                checkNetwork();
            }
        });
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        ((Dumpling_MainActivity)getActivity()).mTemplateTitleText.setText("包饺子");
        hideViewForGone(getActivity().findViewById(R.id.dumpling_title_right_btn_treaty));
    }

    @Override
    public void onAntiViolence(View view) {
        super.onAntiViolence(view);
        int i = view.getId();
        if(i == R.id.dumpling_make_main_friend){
            if (!friend_flag) {
                dumpling_make_main_friend.setImageResource(R.drawable.dumpling_baojiaozi_sy_py_dianji);
                friendAnimation();
            }else{
                dumpling_make_main_friend.setImageResource(R.drawable.dumpling_baojiaozi_sy_py_moren);
                hideView(dumpling_make_main_moneyLayout);
                hideView(dumpling_make_main_cardLayout);
                friend_flag = false;
            }
        }else if(i == R.id.dumpling_make_main_money){
            Intent mIntent = new Intent();
            mIntent.setClass(getActivity(), Dumpling_MakeDumplingActivity.class);
            startActivity(mIntent);

            dumpling_make_main_friend.setImageResource(R.drawable.dumpling_baojiaozi_sy_py_moren);
            hideView(dumpling_make_main_moneyLayout);
            hideView(dumpling_make_main_cardLayout);
            friend_flag = false;
        }else if(i == R.id.dumpling_make_main_card){
        	showToast("敬请期待");
//            startCongratulationCardActivity("1");
            dumpling_make_main_friend.setImageResource(R.drawable.dumpling_baojiaozi_sy_py_moren);
            hideView(dumpling_make_main_moneyLayout);
            hideView(dumpling_make_main_cardLayout);
            friend_flag = false;
        }else if(i == R.id.dumpling_make_person){
        	showToast("敬请期待");
//            startCongratulationCardActivity("2");
        }
    }

    /** 跳转贺卡 */
    private void startCongratulationCardActivity(String type) {
        Intent mIntent = new Intent();
        mIntent.putExtra("sendType",type);
        mIntent.setClass(getActivity(), Dumpling_CongratulationCardActivity.class);
        startActivity(mIntent);
    }

    //朋友动画
    private void friendAnimation(){
        showView(dumpling_make_main_moneyLayout);
        showView(dumpling_make_main_cardLayout);
        friend_flag = true;
        animation = AnimationUtils.loadAnimation(getActivity(), R.anim.dumpling_make_dumpling_friend1);
        dumpling_make_main_moneyLayout.setAnimation(animation);
        dumpling_make_main_cardLayout.setAnimation(animation);

        animation.setAnimationListener(new Animation.AnimationListener() {

            @Override
            public void onAnimationStart(Animation animation) {
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                dumpling_make_main_moneyLayout.setAnimation(AnimationUtils.loadAnimation(getActivity(), R.anim.dumpling_make_dumpling_friend2));
                dumpling_make_main_cardLayout.setAnimation(AnimationUtils.loadAnimation(getActivity(), R.anim.dumpling_make_dumpling_friend2));
//                dumpling_make_main_moneyLayout.startAnimation(AnimationUtils.loadAnimation(getActivity(), R.anim.dumpling_make_dumpling_friend2));
//                dumpling_make_main_cardLayout.startAnimation(AnimationUtils.loadAnimation(getActivity(), R.anim.dumpling_make_dumpling_friend2));

                animation.setAnimationListener(new Animation.AnimationListener() {
                    @Override
                    public void onAnimationStart(Animation animation) {

                    }

                    @Override
                    public void onAnimationEnd(Animation animation) {

                    }

                    @Override
                    public void onAnimationRepeat(Animation animation) {
                        dumpling_make_main_moneyLayout.setAnimation(AnimationUtils.loadAnimation(getActivity(), R.anim.dumpling_make_dumpling_friend3));
                        dumpling_make_main_cardLayout.setAnimation(AnimationUtils.loadAnimation(getActivity(), R.anim.dumpling_make_dumpling_friend3));
                        dumpling_make_main_moneyLayout.startAnimation(AnimationUtils.loadAnimation(getActivity(), R.anim.dumpling_make_dumpling_friend3));
                        dumpling_make_main_cardLayout.startAnimation(AnimationUtils.loadAnimation(getActivity(), R.anim.dumpling_make_dumpling_friend3));
                    }
                });
            }
        });
    }

    /**
     * 检查网络状态
     */
    private void checkNetwork() {
        if (Dumpling_NetWorkUtil.getNetWork(getActivity())) {
            noNet.setVisibility(View.VISIBLE);
            hasNet.setVisibility(View.GONE);
        } else {
            hasNet.setVisibility(View.VISIBLE);
            noNet.setVisibility(View.GONE);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        dumpling_make_main_friend.setImageResource(R.drawable.dumpling_baojiaozi_sy_py_moren);
        friend_flag = false;
        hideView(dumpling_make_main_moneyLayout);
        hideView(dumpling_make_main_cardLayout);
    }
}
